// pages/web/web.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:'',//初始化的url
    url: '', //编辑后的url
    shareUrl: '',//分享url
    image:'',//分享图片
    title:'偶爱登亿',
    loginSuccess:0,
  },
  //网页向小程序传递参数
  bindmessage: function(e) {
    console.log('bindmessage');
    console.log(e)
    //接受h5页面传过来的参数
    var data = e.detail.data;
    if(data){
      var shareData = data[0].url;
      var shareTitle = data[0].title;
      var image = data[0].image;
      this.setData({
        shareUrl: shareData,
        image: image,
        title: shareTitle
      })
      console.log(this.data.shareUrl)
    }
  },
  //网页加载成功
  bindload: function(res) {

  },
  //网页加载失败
  binderror: function(res) {

  },
  reloadUrl: function(url){
    console.log(url);
    var newUrl = url;
    var userinfo = wx.getStorageSync(app.globalData.userInfoKey);
    console.log(userinfo);
    if (url.indexOf('?') > 0) {
      newUrl = newUrl + '&terminal=mini_apps';
      // newUrl = 'http://m.ltrip.com/payPrize2019/index/10/d73fd4e6446e0dd7d1860d7ac34c52ee?' + '&terminal=mini_apps';
    } else {
      newUrl = newUrl + '?terminal=mini_apps';
      // newUrl = 'http://m.ltrip.com/payPrize2019/index/10/d73fd4e6446e0dd7d1860d7ac34c52ee'  +'?terminal=mini_apps';
    }
    if (userinfo.token) {
      newUrl = newUrl + '&token=' + userinfo.token;
    }
    console.log(newUrl)
    //默认分享链接,h5如果传值换成h5的链接
    var shareUrl = 'pages/web/web?url=' + tool.base64encode(url);
    this.setData({
      baseUrl: url,
      shareUrl: shareUrl,
      url: newUrl
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var url = tool.base64decode(options.url);
    this.reloadUrl(url);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成D
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.loginSuccess){
      console.log(this.data.baseUrl);
      this.reloadUrl(this.data.baseUrl);
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this;
    if (that.data.image){
      return {
        title: that.data.title,
        path: that.data.shareUrl,
        imageUrl: that.data.image,
      }
    }else{
      return {
        title: that.data.title,
        path: that.data.shareUrl,
      }
    }
  }
})